package com.wgod.demo.business.service;

import com.wgod.security.common.biz.BaseService;
import org.apache.commons.lang3.StringUtils;
    import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.wgod.demo.business.entity.UserEntity;
import com.wgod.demo.business.mapper.UserMapper;
import com.wgod.demo.business.model.cond.UserCond;
import com.wgod.demo.common.constant.ErrorConstant;
import com.wgod.demo.common.exception.BusinessException;
import com.wgod.demo.common.utils.TaleUtils;

/**
 * Created by WGod on 2018/4/20.
 */
@Service
public class UserService extends BaseService<UserMapper,UserEntity> {


    @Transactional
    public int updateUserInfo(UserEntity user) {
        if (null == user.getUid())
            throw BusinessException.withErrorCode("用户编号不可能为空");
        return mapper.updateUserInfo(user);
    }

    public UserEntity getUserInfoById(Integer uId) {
        return mapper.getUserInfoById(uId);
    }

    public UserEntity login(String username, String password) {
        if (StringUtils.isBlank(username) || StringUtils.isBlank(password))
            throw BusinessException.withErrorCode(ErrorConstant.Auth.USERNAME_PASSWORD_IS_EMPTY);

        String pwd = TaleUtils.MD5encode(username + password);
        UserEntity user = mapper.getUserInfoByCond(username, pwd);
        if (null == user)
            throw BusinessException.withErrorCode(ErrorConstant.Auth.USERNAME_PASSWORD_ERROR);

        return user;
    }


}
